define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {


        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'border/index' + location.search,
                    add_url: 'border/add',
                    edit_url: 'border/edit',
                    del_url: 'border/del',
                    multi_url: 'border/multi',
                    import_url: 'border/import',
                    table: 'border',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'c_name', title: __('Custom.name'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'c_phone', title: __('Custom.phone'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'c_sex', title: __('Custom.sex'), operate: false, table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'business.name', title: __('Business.name'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'phone_code', title: __('Phone_code'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'is_sub_enum', title: __('Is_sub_enum'), operate: false, table: table, class: 'autocontent', formatter: Table.api.formatter.isTrue},
                        {field: 'min_charge', title: __('Min_charge'), operate:'BETWEEN', formatter: Table.api.formatter.nullData},
                        {field: 'reg_datetime', title: __('Reg_datetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, },
                        {field: 'business.contract', title: __('Business.contract'), formatter: Controller.api.formatter.contract},
                        {field: 'expire_datetime', title: __('Expire_datetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime,datetimeFormat: "YYYY-MM-DD"},
                        {field: 'store.name', title: __('Store.name'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'admin.jnum', title: __('Admin.jnum'), operate: 'LIKE',},
                        {field: 'admin.nickname', title: __('Admin.nickname'), operate: 'LIKE'},
                        {field: 'admin.mobile', title: __('Admin.mobile'), operate: 'LIKE'},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'border/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '140px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'border/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'border/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            $("#c-s_id").change(function () {
                $("#c-a_id_text").val('');
                $("#c-a_id").val('');
            })
            $("#c-a_id").data("params", function (obj) {
                //obj为SelectPage对象
                return {custom: {sid: $("#c-s_id").val()}};
            });
            Controller.api.bindevent();
        },
        edit: function () {
            $("#c-s_id").change(function () {
                $("#c-a_id_text").val('');
                $("#c-a_id").val('');
            })
            $("#c-a_id").data("params", function (obj) {
                //obj为SelectPage对象
                return {custom: {sid: $("#c-s_id").val()}};
            });
            Controller.api.bindevent();
        },
        api: {
            formatter: {
                nullData: function (value, row, index) {
                    if (value === null) {
                        return '-';
                    }
                },
                isTrue: function (value, row, index) {
                    if (value === 1) {
                        return '是';
                    } else {
                        return '否';
                    }
                },
                county: function (value, row, index) {
                    return Config.countyList[value].county;
                },
                contract: function (value, row, index) {
                    return Config.contractList[value];
                },
            },
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
        }
    };
    return Controller;
});
